package com.lishe.lishe.lishe.module.mall.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;

import java.util.ArrayList;

/**
 * Created by Bin on 2018/5/8.
 */

public class CheckGoodsImgAdapter extends PagerAdapter implements View.OnClickListener {

    private ArrayList<String> mImages;
    private Activity activity;
    //点击回调接口
    private ItemClicker mItemClicker;

    public interface ItemClicker {
        void onItemClick();
    }

    public CheckGoodsImgAdapter(Activity activity, ArrayList<String> images, ItemClicker itemClick) {
        this.mImages = images;
        this.mItemClicker = itemClick;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        if (mImages == null) {
            return 0;
        }
        return mImages.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //设置每一页显示的内容
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(container.getContext());
        imageView.setOnClickListener(this);
        ImageLoaderUtils.displayImage(activity, mImages.get(position), imageView, R.drawable.defalut_img);
        container.addView(imageView); // 添加到ViewPager容器
        return imageView;// 返回填充的View对象
    }

    @Override
    public void onClick(View v) {
        if (mItemClicker != null) {
            mItemClicker.onItemClick();
        }
    }

    // 销毁条目对象
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}